import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:xiongan_app_yy/utils/eventBus.dart';
import 'package:xiongan_app_yy/utils/image_utils.dart';

class OutBtnWidget extends StatelessWidget {
  final Color color;
  final String title;
  final VoidCallback callback;

  OutBtnWidget({@required this.title, @required this.callback, this.color});

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Row(
      children: <Widget>[
        Expanded(
            child: Padding(
          padding: EdgeInsets.all(20),
          child: FlatButton(
            child: Text(title),
            color: color,
            onPressed: () {
              callback();
            },
          ),
        )),
      ],
    );
  }
}

class HImgBoxWidget extends StatefulWidget {
  String imgSrc;
  String phone;
  Color boxBg;

  HImgBoxWidget({@required this.phone, @required this.imgSrc, this.boxBg});

  @override
  State<StatefulWidget> createState() {
    return HImgBoxWidgetState();
  }
}

class HImgBoxWidgetState extends State<HImgBoxWidget> {
  Widget imageWidget;
  String image;

  @override
  void initState() {
    super.initState();
    if (widget.imgSrc != "") {
      ImageDownloadUtil.getImageBase64(widget.imgSrc, (data) {
        setState(() {
          image = data;
        });
      });
    }
    EventBus.getDefault().register((data) {
      setState(() {
        image = data;
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    EventBus.getDefault().unregister();
  }

  @override
  Widget build(BuildContext context) {
    imageWidget = isType();
    return Container(
        padding: EdgeInsets.only(top: 10, bottom: 40),
        width: double.infinity,
        color: widget.boxBg,
        child: Center(
          child: Container(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(bottom: 6),
                  width: 80,
                  height: 80,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(80),
                    child: imageWidget,
                  ),
                ),
                Text(
                  widget.phone,
                  style: TextStyle(color: Colors.white, fontSize: 16),
                )
              ],
            ),
          ),
        ));
  }

  Widget isType() {
    if (widget.imgSrc == "") {
      return Icon(
        Icons.person,
        size: 80,
        color: Colors.white,
      );
    } else if (image != null) {
      return Image.memory(
        //base64.decode(image),
        base64Decode(image),
        height: 80,
        width: 80,
        fit: BoxFit.fill,
        gaplessPlayback: true, //防止重绘
      );
    } else {
      return Icon(
        Icons.person,
        size: 80,
        color: Colors.white,
      );
    }
  }
}
